# -*- coding: utf-8 -*-
# @Time        :2025/3/5 上午10:56
# @Author      :文刀水寿
# @File        : 17_爬虫_urllib_代理池.py
"""
 @Description :
"""
import urllib.request
import random

url = 'http://www.baidu.com/s?wd=ip'

headers = {
    'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Mobile Safari/537.36 Edg/133.0.0.0'
}

request = urllib.request.Request(url=url, headers=headers)

proxies_pool = [
    {'http': '117.42.94.5:20115'},
]

proxies = random.choice(proxies_pool)

handler = urllib.request.ProxyHandler(proxies=proxies)

opener = urllib.request.build_opener(handler)

response = opener.open(request)

content = response.read().decode('utf8')

with open('ip.html', 'w', encoding='utf8') as fp:
    fp.write(content)
